﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDE_FIC.DAO;
using SDE_FIC.Models;
using PagedList;

namespace SDE_FIC.Controllers
{
    public class MatriculaController : Controller
    {
        private DBConnect _db = new DBConnect();

        //
        // GET: /Matricula/

        public ActionResult Index(int? pagina)
        {
            int tamanhoPagina = 10;
            int numeroPagina = pagina ?? 1;

            CursoDAO _cursoDAO = new CursoDAO(ref _db);

            Funcionario _funcionario = new Funcionario();
            FuncionarioDAO funcionarioDAO = new FuncionarioDAO(ref _db);

            Matricula _matricula = new Matricula();
            MatriculaDAO matriculaDAO = new MatriculaDAO(ref _db);

            Turma _turma = new Turma();
            TurmaDAO turmasDAO = new TurmaDAO(ref _db); 

            var NomeCompleto = Session["LogedUserNomeCompleto"].ToString();

            if (Session["LogedUserPerfil"].ToString() == "coordenador")
            {
                List<Turma> lTurma = turmasDAO.All().ToList();
                ViewBag.lMatricula = matriculaDAO.All();

                return View("~/Views/Sistema/Matricula/Index.cshtml", lTurma.ToPagedList(numeroPagina, tamanhoPagina));
            }
            else
            {
                _funcionario.IdFuncionario = funcionarioDAO.LoadByNome(NomeCompleto).IdFuncionario;           
                List<Turma> lTurma = turmasDAO.LoadByFuncionario(ref _funcionario, true).ToList();
                ViewBag.lMatricula = matriculaDAO.All();

                if (lTurma == null)
                {
                    return View("~/Views/Sistema/Matricula/Index.cshtml");
                }
                else
                {
                    return View("~/Views/Sistema/Matricula/Index.cshtml", lTurma.ToPagedList(numeroPagina, tamanhoPagina));
                }

            }

        }

        public ActionResult Matricular(int id, int? pagina)        
        {
            
            int tamanhoPagina = 16;
            int numeroPagina = pagina ?? 1;

            Turma turma = new Turma();
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);

            turma = _turmaDAO.LoadById(id);

            MatriculaDAO matriculaDAO = new MatriculaDAO(ref _db);

            List<Matricula> lMatricula = matriculaDAO.LoadByTurma(turma).OrderBy(x => x.Aluno.Nome).ToList();
            
            ViewBag.turma_descricao = turma.Descricao + " | " + turma.Curso.CursoNome;

            ViewBag.turma_idturma = turma.Idturma;

            return View("~/Views/Sistema/Matricula/Matricular.cshtml", lMatricula.ToPagedList(numeroPagina, tamanhoPagina));

        }

        public ActionResult Matriculas(int? pagina, int id)
        {
            int tamanhoPagina = 10;
            int numeroPagina = pagina ?? 1;

            Turma turma = new Turma();
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);

            turma = _turmaDAO.LoadById(id);

            MatriculaDAO matriculaDAO = new MatriculaDAO(ref _db);

            return View("~/Views/Sistema/Matricula/Matriculas.cshtml", matriculaDAO.LoadByTurma(turma).ToPagedList(numeroPagina, tamanhoPagina));

        }

        //
        // GET: /Matricula/Details/5

        public ActionResult Details(int Id = 0)
        {
            MatriculaDAO matriculaDAO = new MatriculaDAO(ref _db);
            Matricula _matricula = new Matricula();
            
            _matricula = matriculaDAO.LoadById(Id);

            Turma turma = new Turma();
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);

            turma = _turmaDAO.LoadById(_matricula.Turma.Idturma);

            ViewBag.turma_descricao = turma.Descricao + " | " + turma.Curso.CursoNome;

            ViewBag.turma_idturma = turma.Idturma;


            if (_matricula == null)
            {             
                return HttpNotFound();
            
            }

            return View("~/Views/Sistema/Matricula/Details.cshtml", _matricula);
        }

        //
        // GET: /Matricula/Create

        public ActionResult Create(int idTurma, int id = 0)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            ViewBag.aluno_idaluno = new SelectList(_alunoDAO.All().ToList(), "idaluno", "nome");

            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);
            //ViewBag.turma_idturma = new SelectList(_turmaDAO.All().ToList(), "idturma", "descricao");
            ViewBag.turma_idturma = _turmaDAO.LoadById(idTurma).Idturma;
            ViewBag.turma_descricao = _turmaDAO.LoadById(idTurma).Descricao + " | " + _turmaDAO.LoadById(idTurma).Curso.CursoNome;

            return View("~/Views/Sistema/Matricula/Create.cshtml");
        }

        //
        // POST: /Matricula/Create

        [HttpPost]
        public ActionResult Create(Matricula matricula)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);
            
            if (ModelState.IsValid)
            {
                MatriculaDAO _matriculaDAO = new MatriculaDAO(ref _db);
                _matriculaDAO.Insert(matricula);
                return RedirectToAction("Index");
            }
            
            ViewBag.aluno_idaluno = new SelectList(_alunoDAO.All().ToList(), "idaluno", "nome", matricula.Aluno.IdAluno);
            ViewBag.turma_idturma = new SelectList(_turmaDAO.All().ToList(), "idturma", "descricao", matricula.Turma.Idturma);
            return View("Matricular", matricula);
        }

        //
        // GET: /Matricula/Edit/5

        public ActionResult Edit(int id = 0)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);
            MatriculaDAO _matriculaDAO = new MatriculaDAO(ref _db);
            Matricula _matricula = new Matricula();

            _matricula = _matriculaDAO.LoadById(id);

            if (_matricula == null)
            {
                return HttpNotFound();
            }

            ViewBag.aluno_idaluno = new SelectList(_alunoDAO.All().ToList(), "idaluno", "nome", _matricula.Aluno.IdAluno);
            ViewBag.turma_idturma = new SelectList(_turmaDAO.All().ToList(), "idturma", "descricao", _matricula.Turma.Idturma);
            return View("~/Views/Sistema/Matricula/Edit.cshtml", _matricula);
        }

        //
        // POST: /Matricula/Edit/5

        [HttpPost]
        public ActionResult Edit(Matricula matricula)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);
            MatriculaDAO _matriculaDAO = new MatriculaDAO(ref _db);

            if (ModelState.IsValid)
            {
                _matriculaDAO.Update(matricula);
                return RedirectToAction("Index");
            }

            ViewBag.aluno_idaluno = new SelectList(_alunoDAO.All().ToList(), "idaluno", "nome", matricula.Aluno.IdAluno);
            ViewBag.turma_idturma = new SelectList(_turmaDAO.All().ToList(), "idturma", "descricao", matricula.Turma.Idturma);
            return View("~/Views/Sistema/Matricula/Edit.cshtml", matricula);
        }

        //
        // GET: /Matricula/Delete/5

        public ActionResult Delete(int id = 0)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);
            MatriculaDAO _matriculaDAO = new MatriculaDAO(ref _db);
            Matricula _matricula = new Matricula();

            _matricula = _matriculaDAO.LoadById(id);

            if (_matricula == null)
            {
                return HttpNotFound();
            }

            ViewBag.aluno_idaluno = new SelectList(_alunoDAO.All().ToList(), "idaluno", "nome", _matricula.Aluno.IdAluno);
            ViewBag.turma_idturma = new SelectList(_turmaDAO.All().ToList(), "idturma", "descricao", _matricula.Turma.Idturma);
            return View("~/Views/Sistema/Matricula/Delete.cshtml", _matricula);
        }

        //
        // POST: /Matricula/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Matricula matricula)
        {
            AlunoDAO _alunoDAO = new AlunoDAO(ref _db);
            TurmaDAO _turmaDAO = new TurmaDAO(ref _db);
            MatriculaDAO _matriculaDAO = new MatriculaDAO(ref _db);

            if (ModelState.IsValid)
            {
                _matriculaDAO.Delete(matricula);
                return RedirectToAction("Index");
            }

            ViewBag.aluno_idaluno = new SelectList(_alunoDAO.All().ToList(), "idaluno", "nome", matricula.Aluno.IdAluno);
            ViewBag.turma_idturma = new SelectList(_turmaDAO.All().ToList(), "idturma", "descricao", matricula.Turma.Idturma);
            return View("~/Views/Sistema/Matricula/Delete.cshtml", matricula);

            
        }


    }
}